﻿using System;
using System.Text;

namespace jQueryCSharp.ui
{
    /// <summary>
    /// Classe Fluent para ui.Dialog
    /// </summary>
    public class DialogBuilder : IjQueryCSharpUIBuilder<DialogBuilder>
    {
        private const string Template = "$(\"{0}{1}\").dialog({2});";
        private const string TemplateOnClick = "$(\"{0}{1}\").click(function(){{$('{2}').dialog({3})}});";
        private readonly Dialog _ui;
        private readonly jQueryCSharpUIBuilder _builder;

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="builder">jQueryCSharpUIBuilder</param>
        public DialogBuilder( jQueryCSharpUIBuilder builder )
            : this( builder, "" )
        {
        }

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="identifier"></param>
        public DialogBuilder( jQueryCSharpUIBuilder builder, string identifier )
        {
            _ui = new Dialog( identifier );
            _builder = builder;
        }

        #region Métodos
        public DialogBuilder ID( string value )
        {
            _ui.ID = value;
            return this;
        }

        public DialogBuilder Class( string value )
        {
            _ui.Class = value;
            return this;
        }

        public DialogBuilder Disabled( bool value )
        {
            _ui.Disabled = value;
            return this;
        }

        public DialogBuilder AutoOpen( bool value )
        {
            _ui.AutoOpen = value;
            return this;
        }

        public DialogBuilder OpenOnClick( string value )
        {
            _ui.OpenOnClick = value;
            return this;
        }

        public DialogBuilder AddButton( string value, string function )
        {
            _ui.Buttons.Add( value, function );
            return this;
        }

        public DialogBuilder CloseOnEscape( bool value )
        {
            _ui.CloseOnEscape = value;
            return this;
        }

        public DialogBuilder CloseText( string value )
        {
            _ui.CloseText = value;
            return this;
        }

        public DialogBuilder Draggable( bool value )
        {
            _ui.Draggable = value;
            return this;
        }

        public DialogBuilder Height( int value )
        {
            _ui.Height = value;
            return this;
        }

        public DialogBuilder Hide( string value )
        {
            _ui.Hide = value;
            return this;
        }

        public DialogBuilder MaxHeight( int value )
        {
            _ui.MaxHeight = value;
            return this;
        }

        public DialogBuilder MaxWidth( int value )
        {
            _ui.MaxWidth = value;
            return this;
        }

        public DialogBuilder Modal( bool value )
        {
            _ui.Modal = value;
            return this;
        }

        /// <summary>
        /// 'center', 'left', 'right', 'top', 'bottom'. 
        /// </summary>
        public DialogBuilder AddPosition( string value )
        {
            _ui.Position.Add( value );
            return this;
        }

        public DialogBuilder Resizable( bool value )
        {
            _ui.Resizable = value;
            return this;
        }

        public DialogBuilder Show( string value )
        {
            _ui.Show = value;
            return this;
        }

        public DialogBuilder Title( string value )
        {
            _ui.Title = value;
            return this;
        }

        public DialogBuilder Width( int value )
        {
            _ui.Width = value;
            return this;
        }

        public DialogBuilder ZIndex( int value )
        {
            _ui.ZIndex = value;
            return this;
        }
        #endregion

        #region Override
        public new jQueryCSharpUIBuilder ToString( )
        {
            var sb = new StringBuilder( );

            var sinal = String.IsNullOrEmpty( _ui.Class ) ? "#" : ".";
            var id = String.IsNullOrEmpty( _ui.Class ) ? _ui.ID : _ui.Class;

            if ( String.IsNullOrEmpty( _ui.OpenOnClick ) )
            {
                sb.AppendFormat( Template, sinal, id, _ui );

            }
            else
            {
                sb.AppendFormat( TemplateOnClick, sinal, id, _ui.OpenOnClick, _ui );
            }

            _builder.Append( sb.ToString( ) );

            return _builder;
        }
        #endregion
    }
}
